import { Component, OnInit, Input, OnDestroy } from '@angular/core'
import { NzMessageService } from 'ng-zorro-antd'
import { Router, ActivatedRoute } from '@angular/router'
import {
  IChargeBackOperateReq,
  IChargeBack,
  EChargeBackTransCode,
  EChargeBackProcessType,
} from '@shared/interfaces/operation'
import { ChargeBackDataService } from '../../charge-back.data.service'
import { OperateBaseAbstractClass } from '../operate-base.abstract'
import { I18NService } from '@core'
import { EPaymentBrand } from '@shared/enums'
import { OperationsService } from 'app/routes/operations/operations-abstract.service'
import { Observable } from 'rxjs'
import { IGeneralResponse } from '@shared/interfaces/response'

@Component({
  selector: 'app-chargeback-chargeback-operate',
  templateUrl: './chargeback-operate.component.html',
})
export class ChargebackOperateComponent extends OperateBaseAbstractClass implements OnInit, OnDestroy {
  @Input() chargeBackDetail: IChargeBack
  @Input() transCode: EChargeBackTransCode

  processType: EChargeBackProcessType = EChargeBackProcessType.Close

  EChargeBackProcessType = EChargeBackProcessType
  EPaymentBrand = EPaymentBrand

  constructor(
    public msg: NzMessageService,
    public i18n: I18NService,
    private router: Router,
    private route: ActivatedRoute,
    private operateService: OperationsService,
    private chargeBackDataService: ChargeBackDataService,
  ) {
    super(msg, i18n)
  }

  ngOnInit() {}

  ngOnDestroy() {
    this.chargeBackDataService.resetOperateBody()
  }

  cancel(): void {
    this.router.navigate(['..'], { relativeTo: this.route })
  }

  submit(): void {
    const operation$: Observable<IGeneralResponse> = this.genOperation()
    if (!operation$) {
      return
    }
    operation$.subscribe(
      result => {
        this.router.navigate(['..'], { relativeTo: this.route })
      },
      err => {
        // 暂时隐去前端报错
        // this.msg.error(this.trans('GENERAL.NOTIFICATION.OPERATION_FAIL'))
      },
    )
  }

  genOperation(): Observable<IGeneralResponse> {
    switch (this.processType) {
      case EChargeBackProcessType.Close:
        return this.closeOperate()
      case EChargeBackProcessType.MerchantDebit:
        return this.debitOperate()
      case EChargeBackProcessType.Representment:
        return this.representOperate()
      case EChargeBackProcessType.DebitAndRepresent:
        return this.debitAndRepresent()
    }
  }

  closeOperate(): Observable<IGeneralResponse> {
    const { remark } = this.chargeBackDataService.operateBody
    const isValid = this.checkValidAndShowMsg({ remark })
    if (!isValid) {
      return
    }
    const { disputeNum } = this.chargeBackDetail
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      processType: EChargeBackProcessType.Close,
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }

  debitOperate(): Observable<IGeneralResponse> {
    const { amt, remark } = this.chargeBackDataService.operateBody
    const isValid = this.checkValidAndShowMsg({ remark })
    if (!isValid) {
      return
    }
    const { disputeNum } = this.chargeBackDetail
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      transAmt: amt,
      processType: EChargeBackProcessType.MerchantDebit,
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }

  representOperate(): Observable<IGeneralResponse> {
    const { amt, remark, reasonCode, docIndicator, MMT } = this.chargeBackDataService.operateBody
    const reqBody = this.checkIsAmex() ? { remark, reasonCode } : { remark, reasonCode, docIndicator, MMT }
    const isValid = this.checkValidAndShowMsg(reqBody)
    if (!isValid) {
      return
    }
    const { disputeNum } = this.chargeBackDetail
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      processType: EChargeBackProcessType.Representment,
      reasonCode,
      docIndicator,
      memMessTxt: MMT,
      transAmt: amt,
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }

  debitAndRepresent(): Observable<IGeneralResponse> {
    const { remark, reasonCode, amt, MMT, docIndicator } = this.chargeBackDataService.operateBody
    const reqBody = this.checkIsAmex() ? { remark, reasonCode } : { remark, reasonCode, docIndicator, MMT }
    const isValid = this.checkValidAndShowMsg(reqBody)
    if (!isValid) {
      return
    }
    const { disputeNum } = this.chargeBackDetail

    let transAmt: number
    if (typeof amt === 'string') {
      transAmt = parseFloat(amt)
    }
    const req: IChargeBackOperateReq = {
      disputeNum,
      remark,
      reasonCode,
      transAmt,
      processType: EChargeBackProcessType.DebitAndRepresent,
    }
    if (!this.checkIsAmex()) {
      req.memMessTxt = MMT
      req.docIndicator = docIndicator
    }
    return this.operateService.handleChargeBack(req, this.transCode)
  }

  checkIsAmex(): boolean {
    return this.chargeBackDetail.origTrans.paymentBrand === EPaymentBrand.AME
  }
}
